package com.xtx.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xtx.context.BaseContext;
import com.xtx.dto.OrderPageDto;
import com.xtx.entity.Order;
import com.xtx.entity.OrderDetail;
import com.xtx.mapper.OrderMapper;
import com.xtx.result.PageResult;
import com.xtx.service.OrderService;
import com.xtx.vo.OrderVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    private static final Logger log = LoggerFactory.getLogger(OrderServiceImpl.class);
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public PageResult page(OrderPageDto orderPageDto) {
        PageHelper.startPage(orderPageDto.getPage(), orderPageDto.getPageSize());
        Page<OrderVo> page = orderMapper.page(orderPageDto, BaseContext.getCurrentId());
        Long total = page.getTotal();
        log.info("total: " + total);
        List<OrderVo> records = page.getResult();
        // 遍历订单列表，为每个订单填充orderDetailLis
        for (OrderVo orderVo : records) {
            // 通过orderId查询订单详情
            List<OrderDetail> details = orderMapper.getOrderDetailsByOrderId(orderVo.getOrderId());
            orderVo.setOrderDetailLis(details);
        }
        return new PageResult(total, records);
    }
}
